//本文件填写ArgBox示例-3调用变量-人物-战斗跳转(Clone)事件执行周期内相关阶段函数
//【记得经常使用 Ctrl+S 保存修改，只有保存以后才能在编辑事件时生效】
//可引用变量 ArgBox - 该事件从上层事件继承的参数盒子

/// <summary>
/// 在此区域填写自定义的using指令
/// 注意：一般事件制作不需要填写; 在你明确知道每个using指令的意义和有明确使用需求的情况下再填写此指令集
/// </summary>
#region CustomUsings
public class Foo
{
    public static List<List<List<short>>> _combatSkillTemplateIdsByLevel;
}
public class RandomLevelGenerator
{
    public static List<List<int>> LevelProbForProgress = new List<List<int>> {
        new List<int> {65, 20, 10, 5, 0 , 0, 0, 0, 0},
        new List<int> {50, 30, 20, 10, 5 , 0, 0, 0, 0},
        new List<int> {30, 30, 20, 10, 10, 0, 0, 0, 0},
        new List<int> {20, 25, 25, 10, 10,10, 0, 0, 0},
        new List<int> {20, 20, 20, 10, 10,10,10, 0, 0},
        new List<int> {15, 15, 15, 15,10,10,10,10, 0},
        new List<int> {10, 10, 15, 15,10,10,10,10,10},
        new List<int> {5 , 10, 10, 15,15,15,10,10,10},
        new List<int> {5 , 5,  5,  15,15,15,15,15,10},
    };

    public static int getRandomLevel(Random rng, int stage)
    {
        int total = 0;
        LevelProbForProgress[stage].ForEach((int v) =>
        {
            total += v;
        });
        float acc = 0;
        List<float> accumulate_probs = new List<float>();
        LevelProbForProgress[stage].ForEach((int v) =>
        {
            acc += (float)v / total;
            accumulate_probs.Add(acc);
        });

        var v = rng.NextDouble();
        int level = 0;
        for (int i = 0; i < 9; i++)
        {
            if (v <= accumulate_probs[i])
            {
                return i;
            }
        }
        // accumulate_probs.ForEach((float thr) => {
        // 	if (v <= thr)
        // 	{
        // 		return level;
        // 	}
        // 	level += 1;
        // });
        return -1;
    }
}
#endregion

/// <summary>
/// 该事件监听的触发点被触发后，该接口检测事件是否满足执行条件，如果不满足则不执行该事件
/// </summary>
/// <returns>true - 可以执行 false - 忽略触发不执行</returns>
public override bool OnCheckEventCondition()
{
    return true;
}

// private List<List<short>> _combatSkillTemplateIdsByLevel;

// public List<List<short>> CombatSkillTemplateIdsByLevel
// {
// 	get
// 	{
// 		if (_combatSkillTemplateIdsByLevel.Empty)
// 		{
// 			EventHelper.Log("start init _combatSkillTemplateIdsByLevel");
// 			for (int grade = 0; grade < 9; grade++)
// 			{
// 				_combatSkillTemplateIdsByLevel.Add(new List<short>());
// 				for (int i = 0; i < CombatSkill.Instance.Count; i++)
// 				{
// 					CombatSkillItem skillConfig = CombatSkill.Instance[i];
// 					if (skillConfig.Grade == grade)
// 					{
// 						EventHelper.Log($"{skillConfig.TemplateId} grade = {grade}");
// 						_combatSkillTemplateIdsByLevel[grade].Add(skillConfig.TemplateId);
// 					}
// 				}
// 			}
// 		}
// 		return _combatSkillTemplateIdsByLevel;
// 	}
// }

/// <summary>
/// 该事件被触发且满足执行条件，事件显示前调用
/// 一般用于向参数盒子中准备事件链需要用到的参数
/// </summary>
public override void OnEventEnter()
{
	Random rng = new Random();
	if (Foo._combatSkillTemplateIdsByLevel == null) {
		Foo._combatSkillTemplateIdsByLevel = new List<List<List<short>>>();
		for (int i = 0; i < 9; i++)
		{
			var arr = new List<List<short>>();
			for (int j = 0; j < 5; j++)
			{
				arr.Add(new ());
			}
			Foo._combatSkillTemplateIdsByLevel.Add(arr);
		}
		for (int i = 0; i < Config.CombatSkill.Instance.Count; i++)
		{
			Config.CombatSkillItem skillConfig = Config.CombatSkill.Instance[i];
			if (723 <= skillConfig.TemplateId && skillConfig.TemplateId <= 879)
			{
				EventHelper.Log($"Skip {skillConfig.TemplateId} {skillConfig.Name}");
			}
			else
			{
				var grade = skillConfig.Grade;
				var equipType = skillConfig.EquipType;
				Foo._combatSkillTemplateIdsByLevel[grade][equipType].Add(skillConfig.TemplateId);
				EventHelper.Log($"{skillConfig.TemplateId} {skillConfig.Name} equipType = {equipType} grade = {grade}");
			}
		}
	}
	var _combatSkillTemplateIdsByLevel = Foo._combatSkillTemplateIdsByLevel;//new List<List<short>>();
	var taiwu = ArgBox.GetCharacter(EventArgBox.RoleTaiwu);
	for (int i = 1; i <= 5; i++)
	{
		bool[] pageIsDirect = new bool[]
		{
			true, // rng.NextDouble() > 0.5,
			true, // rng.NextDouble() > 0.5,
			true, // rng.NextDouble() > 0.5,
			true, // rng.NextDouble() > 0.5,
			true, // rng.NextDouble() > 0.5
		};
		int skillLevel = RandomLevelGenerator.getRandomLevel(rng, (GameData.Domains.DomainManager.World.GetMainStoryLineProgress() - 100) / 5);
		int skillIndex = rng.Next(_combatSkillTemplateIdsByLevel[skillLevel][i - 1].Count);
		var skillId = _combatSkillTemplateIdsByLevel[skillLevel][i - 1][skillIndex];
		ItemKey combatSkillBook = EventHelper.GetCombatSkillBook(
			taiwu.GetId(),
			skillId,
			(sbyte)rng.Next(5),
			pageIsDirect,
			5);
		EventHelper.RemoveInventoryItem(taiwu, combatSkillBook, 1, false); // 暂时从背包里删除该书
		ArgBox.Set($"CSB{i}", combatSkillBook);
		ArgBox.Set($"CS{i}", skillId);
		EventHelper.Log($"CS{i}: skill= {skillId}");
	}
	ArgBox.Get("CSB1", out ItemKey csb1);
	ArgBox.Get("CSB2", out ItemKey csb2);
	ArgBox.Get("CSB3", out ItemKey csb3);
	ArgBox.Get("CSB4", out ItemKey csb4);
	ArgBox.Get("CSB5", out ItemKey csb5);
	EventHelper.ShowGetItemPageForItems(new List<ValueTuple<ItemKey, int>>
	{
		new(csb1, 1),
		new(csb2, 1),
		new(csb3, 1),
		new(csb4, 1),
		new(csb5, 1),
	}, "2c5d118a-db29-4baa-a87e-379a1c5a2e48", ArgBox);
}

/// <summary>
/// 该事件执行完毕，即将退出该事件时调用
/// 一般用于从参数盒子中移除事件链中不需要用到的参数，或记录事件触发月份，确保后续触发几率计算
/// </summary>
public override void OnEventExit()
{
    //在事件结束时移除战斗的结果参数。
    // ArgBox.Remove<int>("CombatResult");
}

/// <summary>
/// 该事件即将显示前调用,获取被替换占位符后的事件显示内容
/// 如果事件文本预设满足系统默认的占位符替换方案，则可以不填写此接口
/// </summary>
public override string GetReplacedContentString()
{
    //TODO
    return string.Empty;
}

/// <summary>
/// 获取额外格式化使用的多语言字段
/// </summary>
/// <returns></returns>
public override List<string> GetExtraFormatLanguageKeys()
{
    return default;
}
